
package algo_annotations.ihm;

import algo_annotations.data.Data_a;
import algo_annotations.data.pre_trt.PreTraitementInputData_a;
import algo_annotations.data.sites.Site;
import algo_annotations.data.sites.SousGroupeSite;
import algo_annotations.io.out.DataOutput;
import algo_annotations.trt.DialogTraitementAnno;
import commons.Constants;
import commons.params_appli.ParamsAppli;
import commons.errors.ErrorsHandler;
import commons.logs.Logger;
import commons.utils.Utils;
import commons.utils.UtilsFile;
import java.io.File;
import commons.utils.UtilsTime;
import java.awt.Cursor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class MainFrameTrtAnno extends javax.swing.JFrame {

	private PanelCartoTocTools_a panelCartoTocTools;
	private Data_a data;
	private String titreBase;
	

    public MainFrameTrtAnno() {
        initComponents();
		panelCartoTocTools = new PanelCartoTocTools_a();
		panelContainer_CTT.add(panelCartoTocTools, java.awt.BorderLayout.CENTER);
		panel_modeInteractif.setVisible(false);

		tf_RepBase.setText(ParamsAppli.getParam("common.REPERTOIRE_DONNEES"));
		initListeRepertoiresData();

		titreBase = "Autogene " + Constants.VERSION_AUTOGENE + " : Annotations";
		
		if (ParamsAppli.hasLimitationForAnno)
			titreBase = "DEMO " + titreBase;

				
		// Affichage régulier de la consommation de mémoire
		Timer timer=new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {showMemoryUsage();}
		}, 0, 1000);
		
		Logger.keepLogInRAM();
    }

	private void showMemoryUsage() {
		// Mémoire totale allouée
		long totalMemory = Runtime.getRuntime().totalMemory(); 
		// Mémoire utilisée
		long currentMemory = totalMemory-Runtime.getRuntime().freeMemory();
		
		double prct = 100.0 * currentMemory / totalMemory;
		
		//String newtitre = titreBase + "  -  Mémoire : " + (int)(currentMemory / (1024.0*1024.0)) + " / "  + (int)(totalMemory / (1024.0*1024.0)) + " Mio  (" + (int)prct + " %)";
		String newtitre = titreBase + "  -  Mémoire : " + (int)(currentMemory / (1000.0*1000.0)) + " / "  + (int)(totalMemory / (1000.0*1000.0)) + " Mo";

		this.setTitle(newtitre);
	}


	private void initListeRepertoiresData() {
		combo_JeuDonnees.removeAllItems();
		combo_JeuDonnees.addItem("");
		File rep = new File (tf_RepBase.getText());
		File[] listeAllFiles = rep.listFiles();
		if (listeAllFiles != null) {
			for (File f : listeAllFiles) {
				if (f.isDirectory()) {
					combo_JeuDonnees.addItem(f.getName());
				}
			}
		}
	}


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelContainer_CTT = new javax.swing.JPanel();
        panel_Controls = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        combo_JeuDonnees = new javax.swing.JComboBox();
        tf_RepBase = new javax.swing.JTextField();
        button_ChargerDonnees = new javax.swing.JButton();
        button_ChangeRepBase = new javax.swing.JButton();
        button_LanceGeneralisation = new javax.swing.JButton();
        button_SaveAndQuit = new javax.swing.JButton();
        cb_ModeDebug = new javax.swing.JCheckBox();
        panel_modeInteractif = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        btn_Pass2_selectedAnno = new javax.swing.JButton();
        tf_nbIter = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_tailleVivier = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        button_Step10 = new javax.swing.JButton();
        button_Step1 = new javax.swing.JButton();
        button_InitPersoParams = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tf_pass2_seuilInteraction = new javax.swing.JTextField();
        button_InitAlgo = new javax.swing.JButton();
        btn_Pass2_allAnno = new javax.swing.JButton();
        tf_NbStep = new javax.swing.JTextField();
        tf_pass2_seuilNote = new javax.swing.JTextField();
        button_StepN = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        button_ShowLogs = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelContainer_CTT.setLayout(new java.awt.BorderLayout());

        jLabel6.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel6.setText("Rép. de base : ");

        jLabel7.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel7.setText("Projets :");

        combo_JeuDonnees.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        combo_JeuDonnees.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        combo_JeuDonnees.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_JeuDonneesActionPerformed(evt);
            }
        });

        tf_RepBase.setEditable(false);
        tf_RepBase.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        tf_RepBase.setText("jTextField1");
        tf_RepBase.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));

        button_ChargerDonnees.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        button_ChargerDonnees.setText("Re-charger Données");
        button_ChargerDonnees.setMargin(new java.awt.Insets(2, 4, 2, 4));
        button_ChargerDonnees.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_ChargerDonneesActionPerformed(evt);
            }
        });

        button_ChangeRepBase.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        button_ChangeRepBase.setText("...");
        button_ChangeRepBase.setToolTipText("Changer le répertoire de base");
        button_ChangeRepBase.setMargin(new java.awt.Insets(0, 2, 0, 2));
        button_ChangeRepBase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_ChangeRepBaseActionPerformed(evt);
            }
        });

        button_LanceGeneralisation.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        button_LanceGeneralisation.setText("Lance Trt");
        button_LanceGeneralisation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_LanceGeneralisationActionPerformed(evt);
            }
        });

        button_SaveAndQuit.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        button_SaveAndQuit.setText("Svg. résultat");
        button_SaveAndQuit.setMargin(new java.awt.Insets(2, 4, 2, 4));
        button_SaveAndQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_SaveAndQuitActionPerformed(evt);
            }
        });

        cb_ModeDebug.setText("<html>Mode <br>manuel</html>");
        cb_ModeDebug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_ModeDebugActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel5.setText("seuil interact° :");

        btn_Pass2_selectedAnno.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        btn_Pass2_selectedAnno.setText("selected Anno");
        btn_Pass2_selectedAnno.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btn_Pass2_selectedAnno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_Pass2_selectedAnnoActionPerformed(evt);
            }
        });

        tf_nbIter.setText("100");

        jLabel3.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel3.setText("nb iter :");

        tf_tailleVivier.setText("100");

        jLabel8.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel8.setText("pass n°2 for :");

        button_Step10.setText("x10");
        button_Step10.setMargin(new java.awt.Insets(2, 2, 2, 2));
        button_Step10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_Step10ActionPerformed(evt);
            }
        });

        button_Step1.setText("x1");
        button_Step1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        button_Step1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_Step1ActionPerformed(evt);
            }
        });

        button_InitPersoParams.setText("Start selected Ss-grpe");
        button_InitPersoParams.setMargin(new java.awt.Insets(2, 2, 2, 2));
        button_InitPersoParams.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_InitPersoParamsActionPerformed(evt);
            }
        });

        jLabel1.setText("Step :");

        jLabel4.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel4.setText("seuil note :");

        tf_pass2_seuilInteraction.setText("0.6");

        button_InitAlgo.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        button_InitAlgo.setFocusable(false);
        button_InitAlgo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        button_InitAlgo.setLabel("<html>INIT<br>ALGO</html>");
        button_InitAlgo.setMargin(new java.awt.Insets(2, 2, 2, 2));
        button_InitAlgo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        button_InitAlgo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_InitAlgoActionPerformed(evt);
            }
        });

        btn_Pass2_allAnno.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        btn_Pass2_allAnno.setText("all Anno");
        btn_Pass2_allAnno.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btn_Pass2_allAnno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_Pass2_allAnnoActionPerformed(evt);
            }
        });

        tf_NbStep.setText("40");

        tf_pass2_seuilNote.setText("0.05");

        button_StepN.setText("xN");
        button_StepN.setMargin(new java.awt.Insets(2, 4, 2, 4));
        button_StepN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_StepNActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        jLabel2.setText("vivier :");

        javax.swing.GroupLayout panel_modeInteractifLayout = new javax.swing.GroupLayout(panel_modeInteractif);
        panel_modeInteractif.setLayout(panel_modeInteractifLayout);
        panel_modeInteractifLayout.setHorizontalGroup(
            panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                .addComponent(button_InitAlgo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_tailleVivier, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_nbIter, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(button_InitPersoParams, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_Step1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_Step10))
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addComponent(tf_NbStep, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_StepN, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_pass2_seuilNote, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_pass2_seuilInteraction, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Pass2_allAnno, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Pass2_selectedAnno, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        panel_modeInteractifLayout.setVerticalGroup(
            panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button_InitAlgo, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tf_tailleVivier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(tf_nbIter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_InitPersoParams))
                    .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(button_Step1)
                        .addComponent(jLabel1)
                        .addComponent(button_Step10)
                        .addComponent(jLabel4)
                        .addComponent(tf_pass2_seuilNote, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)
                        .addComponent(tf_pass2_seuilInteraction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panel_modeInteractifLayout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(panel_modeInteractifLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tf_NbStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button_StepN)
                            .addComponent(jLabel8)
                            .addComponent(btn_Pass2_allAnno)
                            .addComponent(btn_Pass2_selectedAnno))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        button_ShowLogs.setFont(new java.awt.Font("Trebuchet MS", 0, 11)); // NOI18N
        button_ShowLogs.setText("Logs...");
        button_ShowLogs.setMargin(new java.awt.Insets(2, 4, 2, 4));
        button_ShowLogs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_ShowLogsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel_ControlsLayout = new javax.swing.GroupLayout(panel_Controls);
        panel_Controls.setLayout(panel_ControlsLayout);
        panel_ControlsLayout.setHorizontalGroup(
            panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_ControlsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panel_ControlsLayout.createSequentialGroup()
                        .addComponent(tf_RepBase, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_ChangeRepBase))
                    .addGroup(panel_ControlsLayout.createSequentialGroup()
                        .addComponent(combo_JeuDonnees, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button_ChargerDonnees, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(button_SaveAndQuit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(button_LanceGeneralisation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(button_ShowLogs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cb_ModeDebug, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_modeInteractif, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panel_ControlsLayout.setVerticalGroup(
            panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_ControlsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_ControlsLayout.createSequentialGroup()
                        .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(tf_RepBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button_ChangeRepBase)
                            .addComponent(button_LanceGeneralisation))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(combo_JeuDonnees, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button_ChargerDonnees)
                            .addComponent(button_SaveAndQuit)
                            .addComponent(jLabel7)))
                    .addGroup(panel_ControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cb_ModeDebug, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(button_ShowLogs)))
                .addContainerGap())
            .addGroup(panel_ControlsLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(panel_modeInteractif, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel_Controls, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelContainer_CTT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panel_Controls, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelContainer_CTT, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void button_Step1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_Step1ActionPerformed
		computeSteps(1);
}//GEN-LAST:event_button_Step1ActionPerformed

	private void button_StepNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_StepNActionPerformed
		if (!Utils.isInteger_Pos(tf_NbStep.getText())) return;
		int nbStep = Integer.parseInt(tf_NbStep.getText());
		
		this.setCursor(Cursor.WAIT_CURSOR);
		computeSteps(nbStep);
		this.setCursor(Cursor.DEFAULT_CURSOR);
	}//GEN-LAST:event_button_StepNActionPerformed

	private void button_InitPersoParamsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_InitPersoParamsActionPerformed
		if (data==null || data.algo==null) return;
		SousGroupeSite sgs = panelCartoTocTools.getPanelTOC().getSelectedTraitement().sgs;
		int tailleVivier = Integer.parseInt(tf_tailleVivier.getText());
		int nbIter = Integer.parseInt(tf_nbIter.getText());
		tf_NbStep.setText(""+nbIter);
		data.initAlgo_forSousGroupe(sgs, tailleVivier, nbIter);
	}//GEN-LAST:event_button_InitPersoParamsActionPerformed

	private void button_InitAlgoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_InitAlgoActionPerformed
		if (data==null) return;
		data.createAndInitAlgo_interactif();
		data.prepareTraitement_pass1_interactif();

		// on affiche la liste des traitements dans la TOC
		// vAllTrt_pass2 est vide ce stade
		panelCartoTocTools.getPanelTOC().showTraitements(data.algo.vAllTrt_pass1, data.algo.vAllTrt_pass2);

		panelCartoTocTools.repaintCarto();
	}//GEN-LAST:event_button_InitAlgoActionPerformed

	private void btn_Pass2_selectedAnnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_Pass2_selectedAnnoActionPerformed
		if (data==null || data.algo==null) return;
		
		if (!Utils.isFloat_Pos(tf_pass2_seuilNote.getText())) return;
		if (!Utils.isFloat_Pos(tf_pass2_seuilInteraction.getText())) return;

		double seuilNote = Double.parseDouble(tf_pass2_seuilNote.getText());
		double seuilInteraction = Double.parseDouble(tf_pass2_seuilInteraction.getText());

		Site siteSelected = panelCartoTocTools. getSelectedSite();

		data.prepareTraitement_pass2_interactif(siteSelected, seuilNote, seuilInteraction);
		panelCartoTocTools.getPanelTOC().showTraitements(data.algo.vAllTrt_pass1, data.algo.vAllTrt_pass2);
		panelCartoTocTools.getPanelTOC().selectLastTraitement();
	}//GEN-LAST:event_btn_Pass2_selectedAnnoActionPerformed

	private void combo_JeuDonneesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_JeuDonneesActionPerformed
		button_ChargerDonneesActionPerformed(null);
}//GEN-LAST:event_combo_JeuDonneesActionPerformed

	private void button_ChargerDonneesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_ChargerDonneesActionPerformed
		if (combo_JeuDonnees.getSelectedItem()==null || combo_JeuDonnees.getSelectedItem().equals("")) return;
		
		String fullPath_JeuDonnee = tf_RepBase.getText() + "\\" + combo_JeuDonnees.getSelectedItem();
		String fullPath_RepFichierSource = fullPath_JeuDonnee + "\\" + ParamsAppli.getParam("common.SOUS_REP_FICHIERS_SOURCE");

		ErrorsHandler.reinitErrors();
		
		// reinit des logs en RAM, pour éviter une fuite de mémoire en cas de très nombreux lancements de traitements
		Logger.resetLogInRAM();

		this.setCursor(Cursor.WAIT_CURSOR);
		Data_a data_new = PreTraitementInputData_a.createDataFromDirectory(this, fullPath_RepFichierSource);
		this.setCursor(Cursor.DEFAULT_CURSOR);

		if (ErrorsHandler.isThereErrors()) {
			ErrorsHandler.addError("Impossible de traiter les données présentes dans le répertoire : " + fullPath_JeuDonnee);
			ErrorsHandler.showErrors();
		}

		if (data_new != null) {
			data = data_new;
			panelCartoTocTools.setData(data);
			panelCartoTocTools.setEchelleCarteCourante(data.params.echelleCible_inv);

			titreBase = "Autogene " + Constants.VERSION_AUTOGENE + " : Annotations    paramètres : " + data.params.nomParam;


			// On pre-rempli les param de traitement interactif avec les paramètres utilisateur pout le traitement auto
			// pour avoir un peu de consistance dans l'IHM
			tf_tailleVivier.setText(""+data.params.algo_tailleVivier_pass1);
			tf_nbIter.setText(""+data.params.algo_nbIter_pass1);

			// Normalement utilisé par le mode interactif, un appel ici permet d'afficher les anno dans leur positions par defaut
			//  peu visible mais c'est mieux que rien
			data.createAndInitAlgo_interactif();
			data.prepareTraitement_pass1_interactif();

			// On affiche dans la TOC la liste des traitements pass1 (ceux de pass2 seront déterminé pendant le traitement ...)
			// Ca permet à l'utilisateur de vérifier le sous-découpage du problème
			panelCartoTocTools.getPanelTOC().showTraitements(data.algo.vAllTrt_pass1, null);
		}

		panelCartoTocTools.repaintCarto();
}//GEN-LAST:event_button_ChargerDonneesActionPerformed

	private void button_LanceGeneralisationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_LanceGeneralisationActionPerformed
		if (data==null) return;

		// Lance la Dlg de traitement complet automatique
		DialogTraitementAnno dlg = new DialogTraitementAnno(data);
		dlg.setModal(true);
		
		// positionnement au centre de this
		int x_centerThis = this.getLocation().x + this.getWidth()/2;
		int y_centerThis = this.getLocation().y + this.getHeight()/2;
		dlg.setLocation(x_centerThis - dlg.getWidth()/2, y_centerThis - dlg.getHeight()/2);
		
		dlg.setVisible(true);


		// En fin de traitement, on affiche dans la TOC la liste des traitements réalisés, pour info
		panelCartoTocTools.getPanelTOC().showTraitements(data.algo.vAllTrt_pass1, data.algo.vAllTrt_pass2);

		// En fin de traitement, on met a jour les donnée à afficher, c'est à dire le bestGroupeAnno
		data.majSPIAnno();
		panelCartoTocTools.repaintCarto();
}//GEN-LAST:event_button_LanceGeneralisationActionPerformed

	private void button_SaveAndQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_SaveAndQuitActionPerformed

		if (data==null)
			return;

		String path_JeuDonnees = tf_RepBase.getText() + "\\" + combo_JeuDonnees.getSelectedItem();

		// On demande dans quel sous répertoire écrire les données = nom du traitement
		//  on propose par défaut un nom pratique
		String nomTrt = "<DATE> ANNO (" + data.params.nomParam + ")";
		nomTrt = JOptionPane.showInputDialog(this, "Indiquer le nom du traitement (il doit constituer un nom de répertoire valide)", nomTrt);

		// Export annulé par l'utilisateur ?
		if (nomTrt==null)
			return;

		// Remplacement de l'éventuelle balise <DATE>
		nomTrt = nomTrt.replaceAll("<DATE>", new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()));

		// Vérification de la validité du nom choisi
		if (!UtilsFile.isFileNameValide(nomTrt)) {
			JOptionPane.showMessageDialog(this, "Nom de traitement incorrect : doit constituer un nom de répertoire valide !", "Erreur", JOptionPane.ERROR_MESSAGE);
			return;
		}

		String path_Export = path_JeuDonnees + "\\" + ParamsAppli.getParam("common.SOUS_REP_FICHIERS_TRAITES") + "\\" + nomTrt;

		this.setCursor(Cursor.WAIT_CURSOR);
		DataOutput.writeResult(data, path_Export);
		this.setCursor(Cursor.DEFAULT_CURSOR);


		//this.setVisible(false);
		//this.dispose();
}//GEN-LAST:event_button_SaveAndQuitActionPerformed

	private void button_Step10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_Step10ActionPerformed
		computeSteps(10);
	}//GEN-LAST:event_button_Step10ActionPerformed

	private void btn_Pass2_allAnnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_Pass2_allAnnoActionPerformed
		if (data==null || data.algo==null) return;

		if (!Utils.isFloat_Pos(tf_pass2_seuilNote.getText())) return;
		if (!Utils.isFloat_Pos(tf_pass2_seuilInteraction.getText())) return;

		double seuilNote = Double.parseDouble(tf_pass2_seuilNote.getText());
		double seuilInteraction = Double.parseDouble(tf_pass2_seuilInteraction.getText());

		data.prepareTraitement_pass2_interactif(seuilNote, seuilInteraction);
		panelCartoTocTools.getPanelTOC().showTraitements(data.algo.vAllTrt_pass1, data.algo.vAllTrt_pass2);
	}//GEN-LAST:event_btn_Pass2_allAnnoActionPerformed

	private void cb_ModeDebugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_ModeDebugActionPerformed
		panel_modeInteractif.setVisible(cb_ModeDebug.isSelected());
	}//GEN-LAST:event_cb_ModeDebugActionPerformed

	private void button_ChangeRepBaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_ChangeRepBaseActionPerformed
		// Changement du répertoire de base des fichiers
		JFileChooser jfc = new JFileChooser();
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		jfc.setCurrentDirectory(new File(tf_RepBase.getText()));
		int returnVal = jfc.showDialog(this, "Choisir ce répertoire");
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			tf_RepBase.setText(jfc.getSelectedFile().getPath());
			initListeRepertoiresData();
		}
	}//GEN-LAST:event_button_ChangeRepBaseActionPerformed

    private void button_ShowLogsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_ShowLogsActionPerformed
        Logger.showLogInRAM(this);
    }//GEN-LAST:event_button_ShowLogsActionPerformed

	private void computeSteps (int nbStep) {
		if (data==null || data.algo==null) return;
		
		UtilsTime.startTimer("==> Traitement " + nbStep + " steps");
		for (int i=0; i<nbStep; i++)
			data.step_forCurrentSousGroupe();
		UtilsTime.stopTimer("==> Traitement " + nbStep + " steps");

		data.majSPIAnno(); // on affiche le best groupe anno issu de ces step
		panelCartoTocTools.repaintCarto();
		Logger.log_INFO2(UtilsTime.getStringDuree("==> Traitement " + nbStep + " steps"));
	}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Pass2_allAnno;
    private javax.swing.JButton btn_Pass2_selectedAnno;
    private javax.swing.JButton button_ChangeRepBase;
    private javax.swing.JButton button_ChargerDonnees;
    private javax.swing.JButton button_InitAlgo;
    private javax.swing.JButton button_InitPersoParams;
    private javax.swing.JButton button_LanceGeneralisation;
    private javax.swing.JButton button_SaveAndQuit;
    private javax.swing.JButton button_ShowLogs;
    private javax.swing.JButton button_Step1;
    private javax.swing.JButton button_Step10;
    private javax.swing.JButton button_StepN;
    private javax.swing.JCheckBox cb_ModeDebug;
    private javax.swing.JComboBox combo_JeuDonnees;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel panelContainer_CTT;
    private javax.swing.JPanel panel_Controls;
    private javax.swing.JPanel panel_modeInteractif;
    private javax.swing.JTextField tf_NbStep;
    private javax.swing.JTextField tf_RepBase;
    private javax.swing.JTextField tf_nbIter;
    private javax.swing.JTextField tf_pass2_seuilInteraction;
    private javax.swing.JTextField tf_pass2_seuilNote;
    private javax.swing.JTextField tf_tailleVivier;
    // End of variables declaration//GEN-END:variables



}
